package org.example;

import redis.clients.jedis.HostAndPort;

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        try {
            ConnectionConfig config = parseArguments(args);
            new FallbackCache(config);
        } catch (IllegalArgumentException e) {
            System.err.println("参数错误: " + e.getMessage());
            printUsage();
            System.exit(1);
        }
    }

    /**
     * 解析运行模式参数，如单机、集群、哨兵
     *
     * @param args args
     * @return {@link ConnectionConfig }
     */
    private static ConnectionConfig parseArguments(String[] args) {
        if (args.length < 1) {
            throw new IllegalArgumentException("缺少运行模式参数");
        }

        String mode = args[0].toLowerCase();
        String[] modeArgs = Arrays.copyOfRange(args, 1, args.length);

        switch (mode) {
            case "single":
                return parseSingleMode(modeArgs);
            case "sentinel":
                return parseSentinelMode(modeArgs);
            case "cluster":
                return parseClusterMode(modeArgs);
            default:
                throw new IllegalArgumentException("未知模式: " + mode);
        }
    }

    /**
     * 解析单机模式
     *
     * @param args args
     * @return {@link ConnectionConfig }
     */
    private static ConnectionConfig parseSingleMode(String[] args) {
        if (args.length != 2) {
            throw new IllegalArgumentException("单机模式需要 host 和 port");
        }
        return new ConnectionConfig(args[0], Integer.parseInt(args[1]));
    }

    /**
     * 解析哨兵模式
     *
     * @param args args
     * @return {@link ConnectionConfig }
     */
    private static ConnectionConfig parseSentinelMode(String[] args) {
        if (args.length < 2) {
            throw new IllegalArgumentException("哨兵模式需要主节点名称和至少一个哨兵地址");
        }
        String master = args[0];
        Set<String> sentinels = Arrays.stream(args)
                .skip(1)
                .map(s -> s.contains(":") ? s : s + ":26379")
                .collect(Collectors.toSet());
        return new ConnectionConfig(master, sentinels);
    }

    /**
     * 解析集群模式
     *
     * @param args args
     * @return {@link ConnectionConfig }
     */
    private static ConnectionConfig parseClusterMode(String[] args) {
        if (args.length == 0) {
            throw new IllegalArgumentException("集群模式需要至少一个节点地址");
        }
        Set<HostAndPort> nodes = Arrays.stream(args)
                .map(addr -> {
                    String[] parts = addr.split(":");
                    return new HostAndPort(
                            parts[0],
                            parts.length > 1 ? Integer.parseInt(parts[1]) : 6379
                    );
                })
                .collect(Collectors.toSet());
        return new ConnectionConfig(nodes);
    }

    private static void printUsage() {
        System.out.println("用法:");
        System.out.println("单机模式:   java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar single <host> <port>");
        System.out.println("哨兵模式: java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar sentinel <主节点名称> <哨兵地址1> [哨兵地址2...]");
        System.out.println("集群模式:  java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar cluster <节点地址1> [节点地址2...]");
        System.out.println("\n示例:");
        System.out.println("单机模式:   java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar single 127.0.0.1 6379");
        System.out.println("哨兵模式: java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar sentinel mymaster 192.168.1.10 192.168.1.11:26379");
        System.out.println("集群模式:  java -jar redisClient-1.0-SNAPSHOT-jar-with-dependencies.jar cluster 10.0.0.1:7000 10.0.0.2:7001");
    }
}